﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RPGXEngine;
using DotNetX;

namespace RPGX.Scripting.SystemFunctions
{
    public static class SystemFunction_NetworkAuthenticate
    {
        public static IVariableType Method(ScriptManager sm, FunctionScope scope, List<IVariableType> data)
        {
            if (data == null || data.Count < 2)
                return sm.Zero;

            IVariableType res = sm.True;

            string username = data[0].Value.ToEmptyStringIfNull().Trim();
            string password = data[1].Value.ToEmptyStringIfNull();

            string errorCallBack = String.Empty; // optional
            if(data.Count > 2)
                errorCallBack = data[2].Value.ToEmptyStringIfNull();

            string successCallBack = String.Empty;
            if (data.Count > 3)
                successCallBack = data[3].Value.ToEmptyStringIfNull();

            try
            {
                Game1.Instance.NetworkManager.Authenticate(username, password, errorCallBack, successCallBack);
            }
            catch (Exception)
            {
                res = sm.False;
            }

            return res;
        }
    }
}
